<template>
  <view class="footer">
    <view class="font">第三方登录</view>
    <view class="list">
      <view class="item" @click="login('qq')">
        <image src="../../static/QQ-circle.png" class="img"></image>
      </view>
      <view class="item" @click="login('weixin')">
        <image src="../../static/weixin-circle.png" class="img"></image>
      </view>
      <view class="item" @click="login('sinaweibo')">
        <image src="../../static/weibo-circle.png" class="img"></image>
      </view>
    </view>
  </view>
</template>

<script setup>
import { toast, checkServe } from '@/common';
import { post } from '@/api';
import { useUser } from '@/store';

const props = defineProps({
  // 是否同意协议
  agree: {
    type: Boolean,
    default: false
  },
})

function bindUserThird(type, access_token, openid, unionid, nickname, avatar){
  const params = {
    type,
    access_token,
    openid,
    unionid,
    nickname,
    avatar
  }
  post('api/user_third/userThirdLogin', params).then(res => {
    console.log(res);
    if(res.code == 1){
      if(res.data.userinfo.mobile){
        const data = res.data.userinfo;
        uni.setStorageSync('token', data.token);
        uni.setStorageSync('userInfo', data);
        useUser().setToken(data.token);
        useUser().setUserInfo(data);
        getUserInfoDetail(data.user_id);
        uni.reLaunch({
          url: '/pages/index/index'
        });
      }else{
        const data = res.data.userinfo;
        uni.setStorageSync('token', data.token);
        useUser().setToken(data.token);
        uni.navigateTo({
          url: `/pages/login/verify?type=${type}`
        });
      }
    }
  })
}
function getUserInfoDetail(id){
  const params = {
    access_user_id: id
  }
  post('api/user/getUserDetail', params).then(res => {
    if(res.code == 1){
      const data = res.data;
      uni.setStorageSync('userDetail', data);
      useUser().setUserDetail(data);
    }
  })
}
function login(value){
  if(!props.agree) return toast('请勾选用户协议');
  if(value == 'sinaweibo') return toast('开发中...');
  
  function setLogin(){
    uni.login({
      provider: value, //使用微信登录
      onlyAuthorize: false,
      success: function (loginRes) {
        uni.getUserInfo({
          provider: value,
          success: function (infoRes) {
            const { access_token, openid } = loginRes.authResult;
            const { nickName, avatarUrl } = infoRes.userInfo;
            const type = {
              'weixin': 1,
              'qq': 2
            }
            const uniId = infoRes.userInfo.unionid ? infoRes.userInfo.unionid : infoRes.userInfo.unionId;
            bindUserThird(type[value], access_token, openid, uniId, nickName, avatarUrl);
          }
        });
      },
      fail(err) {
        console.log(err);
      }
    });
  }
  checkServe('oauth', value, setLogin);
}
</script>

<style lang="scss" scoped>
  .footer{
    position: fixed;
    left: 50%;
    bottom: 100rpx;
    transform: translateX(-50%);
    width: 456rpx;
    border-top: 1px solid #EBEBEB;
    .list{
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .item{
        width: 72rpx;
        height: 72rpx;
      }
    }
    .font{
      font-weight: 400;
      font-size: 24rpx;
      color: #898989;
      text-align: center;
      padding-top: 54rpx;
      padding-bottom: 44rpx;
    }
  }
</style>